/*
 *   This File is Part of CuteOgre Game Engine
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *	Copyright 2010 Alexandre Pires ( c.alexandre.pires@gmail.com )
 *  Created on: 2010/08/22
 */

#include "LogManager.h"
#include "ConfigManager.h"

#include <QCoreApplication>

template<> LogManager* Singleton < LogManager >::ms_Singleton = 0;

void LogManager::init () {

	QString fileName = ConfigManager::readString("LogFile","Game.log");

	mFile.setFileName ( fileName );

	if ( !mFile.open ( QIODevice::WriteOnly | QIODevice::Text ) )
		throw "Cannot open log file!";

	mStream.setDevice ( &mFile );
	mShutdown = false;

	logMessage ( "Log Manager::Starting Up" );
}

void LogManager::shutdown () {

	logMessage ( "Log Manager::Shutting down" );
	mShutdown = true;
	mFile.close ();
}

void LogManager::logException ( const QString& rMessage ) {

	logMessage ( rMessage );
	throw rMessage.toAscii ().data ();
}

void LogManager::logMessage ( const QString& rMessage ) {

	if ( mShutdown )
		return;

	mStream << rMessage << "\n";
	mStream.flush();
}

void LogManager::exception ( const QString& rMessage ) {
	LogManager::getSingleton ().logException ( rMessage );
}

void LogManager::message ( const QString& rMessage ) {
	LogManager::getSingleton ().logMessage ( rMessage );
}
